package com.dongdong.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.dongdong.em.Result;
import com.dongdong.em.ResultEnum;
import com.dongdong.model.TbUser;
import com.dongdong.service.UserService;
import com.dongdong.util.JWTUtil;
import com.dongdong.util.ShiroUtils;
import com.dongdong.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("user")
@RefreshScope  //热更新
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JWTUtil jwtUtil;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired(required = false)//required=false表示当前Bean如果有的话直接注入,没有的话忽略跳过
    private JavaMailSender javaMailSender;//https://blog.csdn.net/Falcon__/article/details/128909127


    @GetMapping("getUserByName")
    public Result getUserByName(@RequestParam String username) {
        TbUser user = userService.getUserByName(username);
        return Result.OK().setData(user);
    }

    //生成验证码
    @GetMapping("generateCode")
    public Result generateCode(@RequestParam String phone) {
        String code = userService.generateCode(phone);

        //取出验证码  返回给前端  赋值给验证码框
        String s = stringRedisTemplate.opsForValue().get(phone);
        return Result.OK().setData(s);
    }


    //注册
    @PostMapping("register")
    public Result register(@RequestBody TbUser user) {
        int i = userService.register(user);
        if (i <= 0) {
            return Result.ZHRESPONSE(ResultEnum.ERROR);
        } else if (i == 999) {//6
            return Result.ZHRESPONSE(ResultEnum.REGISTER_USER_EXIST);//用户已经存在
        }
        return Result.ZHRESPONSE(ResultEnum.REGISTER_SUCCESS_DATA);
    }


    /*找回密码*/
    @GetMapping("forgetPwd")
    public Result forgetPwd(@RequestParam String username) {
        /*根据用户名查询email 先检查数据库中是否存在email数据*/
        TbUser userName = userService.getUserName(username);
        String userEmail = userName.getEmail();
        if (userEmail == null) {
            return Result.ZHRESPONSE(ResultEnum.EMAIL_IS_NULL);//email为空  无法发送信息
        }
        /*使用hutool随机生成六位随机数作为密码，并且重新加密*/
        String randomPwd = RandomUtil.randomNumbers(4);
        /*生成六位数的盐*/
        String salt = ShiroUtils.generateSalt(6);
        /*MD5加密三次*/
        String md5 = ShiroUtils.encryptPassword("md5", randomPwd, salt, 5);
        /*拿到了加密过后的密码md5*/
        /*根据用户名 重置密码、盐*/
        int updateuser = userService.updatePwdSatl(userName.getUsername(), md5, salt);
        /*修改成功，发送邮件通知用户登录*/
        if (updateuser > 0) {
            /*修改成功，发送邮件通知用户登录*/
//          创建发送邮件对象
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//          发件人邮箱
            simpleMailMessage.setFrom(userEmail);
//          定义一个数组 将发件人和收件人放进去
            String[] str = {userEmail};/*从数据库查询到的email根据获取到的email进行发送邮件*/
            simpleMailMessage.setTo(str);
//          设置邮件主题目
            simpleMailMessage.setSubject("重置密码");//标题
            simpleMailMessage.setText("重置成功(●'◡'●),重置后的密码:" + randomPwd);//内容  这里是hutool生成的随机密码
//          设置发送时间
            simpleMailMessage.setSentDate(new Date());
//          执行发送
            javaMailSender.send(simpleMailMessage);
        }
        return Result.OK().setData("发送成功，去看邮箱");
    }


    //设定指定日期 给用户发送生日邮件
    @Scheduled(cron = "0 0 0/24 * * ?")        //设定执行时间  每24小时执行一次
    @PostMapping("SendBirthday")
    public Result SendBirthday() {
        System.out.println("发送生日祝福邮件了-->");
        //1.查询所有用户  获取生日日期  然后判断匹配  合适的日期进行发送
        Result result = userService.getUserByBirthday();
        return result;
    }
}
